#include <stdio.h>
#include <stdint.h>

#include "serial/serial.h"
#include "bytebuffer.h"
#include "dlmssettings.h"
#include "variant.h"
#include "cosem.h"
#include "server.h"
#include "date.h"
#include "gxserializer.h"


#include "ee.h"
#include "dlmsWrap.h"


void test();

int main(int argc, char **argv)
{
    uint8_t buf[512];
    ee_r(0,buf,1);
    test();
    return 0;

}


//Define server system title.
static unsigned char SERVER_SYSTEM_TITLE[8] = { 0 };

void printHex(const char* tag,const uint8_t * buff,uint32_t len)
{
    printf("%s: ",tag);
    for(int i=0;i<len;i++)
    {
        printf("%02x ",buff[i]);
    }
    printf("\n");
}

void test()
{

    DLMSInit();
    uint8_t mtr[]="073112345";
    sendEventNotification(mtr,strlen((char*)mtr),12345);


    printf("test start\n");
    serial::Serial s("\\\\.\\COM21",115200,serial::Timeout(100, 500, 10, 30, 10)); 
    printf("serial::Serial\n");
    //s.open();
    printf("serial::open\n");

    std::vector<unsigned char> buff;

    while(1)
    {
        //printf(".\n");
        DLMSLoop();
        uint8_t buffout[1024];
        if(s.available())
        {
            buff.clear();
            s.read(buff,1024);
            if(buff.size())
            {
                printHex("RX:",buff.data(),buff.size());
                int sendLen=DLMSprocessRecv(buff.data(),buff.size(),buffout);
                if(sendLen)
                {
                    s.write(buffout,sendLen);
                    printHex("TX:",buffout,sendLen);
                }
            }
        }

    }
}